package com.android.file;

import android.os.Environment;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class FileHandler {

	
	private static FileHandler instance;
	
	
	private FileHandler(){
		
	}
	
	
	public static FileHandler getInstance(){
		
		if(instance == null){
			instance = new FileHandler();
		}
		
		return instance;
	}
	
	
	/**
	 * 获取根目录路径
	 * @return
	 */
	public String getRootPath() {
		
		File file = new File("/mnt");
		
		File[] files = file.listFiles();
		
		for(File f: files){
			
			if(f.getName().startsWith("sdcard")){

				if(f.getFreeSpace()>0){
					
					return f.getAbsolutePath();
				}
			}
		}
		
		return Environment.getExternalStorageDirectory().toString();
	}
	

	
	/**
	 * 检查SDCARD 状态是否已经挂载
	 * 
	 * @return
	 */
	public boolean checkSDCardState() {

		String sState = Environment.getExternalStorageState();

		return sState.equals(Environment.MEDIA_MOUNTED);
	}

	
	/**
	 * 检查路径是否存在
	 * @param path
	 * @return
	 */
	public boolean isExsit(String path) {

		File file = new File(getRootPath() + "" + path);

		return file.exists();
	}
	

	/**
	 * 检查上一目录是否存在
	 * @param path
	 * @param create
	 * @return
	 */
	public boolean checkParentExsit(String path, boolean create) {

		File file = new File(getRootPath() + "" + path);

		File parent = file.getParentFile();

		if (parent.exists()) {
			return true;
		}

		if (create) {
			try {
				if (parent.mkdirs()) {
					return createFile(parent.getAbsolutePath() + "/.nomedia");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return false;
	}

	
	/**
	 * 创建文件
	 * @param path
	 * @return
	 */
	public boolean createFile(String path) {

		File file = new File(path);
		if (!file.exists()) {
			try {
				return file.createNewFile();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	
	/**
	 * 创建文件夹
	 * @param path
	 * @return
	 */
	public boolean createDirectory(String path) {

		File file = new File(getRootPath() + path);

		if (!file.exists()) {
			return file.mkdirs();
		}

		return true;
	}
	
	/**
	 * 创建新的文件夹
	 * @param
	 * @return
	 */
	public boolean NCreateDirectory(String path) {

		File file = new File(Environment.getExternalStorageDirectory().toString() + path);

		if (!file.exists()) {
			return file.mkdirs();
		}

		return true;
	}

	
	/**
	 * 创建文件，假如已经存在，先删除，然后再创建
	 * @param path
	 * @return
	 */
	public File createNewFile(String path) {

		File file = new File(getRootPath() + path);
		if (file.exists()) {
			file.delete();
		}

		try {
			file.createNewFile();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return file;
	}
	
	/**
	 * 创建文件，假如已经存在，先删除，然后再创建
	 * @param path
	 * @return
	 */
	public File NCreateNewFile(String path) {

		File file = new File(Environment.getExternalStorageDirectory().toString() + path);
		if (file.exists()) {
			file.delete();
		}

		try {
			file.createNewFile();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return file;
	}

	/**************************************************************************************
	 * 
	 * @param path
	 * @return
	 */
	public InputStream read(String path) {
		
		File file = new File(getRootPath() + "" + path);

		if (file.exists()) {

			try {
				return new FileInputStream(file);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return null;
	}
	
	

	public boolean write(String path, byte[] body, boolean append) {

		File file = new File(getRootPath() + "" + path);
		try {

			if (!file.exists()) {

				if (checkParentExsit(path, true)) {
					file.createNewFile();
				}
			}

			FileOutputStream output = new FileOutputStream(file,append);
			output.write(body);

			output.flush();
			output.close();

			return true;

		} catch (Exception e) {
			e.printStackTrace();
		}

		return false;
	}
	
	
	public byte[] readBytes(InputStream in) {
		ByteArrayOutputStream baos = null;
		int c = 0;
		try {
			baos = new ByteArrayOutputStream();
			while ((c = in.read()) != -1) {
				baos.write((byte) c);
			}

			return baos.toByteArray();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				baos.close();
				in.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			baos = null;
			in = null;
		}	 

		return null;
	}

	
	
	public String[] getFileList(String path){
		
		File file = new File(getRootPath() + path);
		if(file.exists() && file.isDirectory()){
			return file.list();
		}
		
		return null;
	}
	
	
	
	public boolean delete(String path){
		
		File file = new File(getRootPath()+path);
		
		if(file.exists()){
			return file.delete();
		}
		
		return false;
	}
	
	
}
